package com.jtfr.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jtfr.pojo.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags="1.1", value="用户管理")
@RestController
@RequestMapping("/users")
public class UserController {
	
	private static Logger LOGGER = LoggerFactory.getLogger(UserController.class);
	
	@GetMapping
	@ApiOperation(value = "条件查询(DONE)") // @ApiOperation 对接口的描述
	@ApiImplicitParams({
		// @ApiImplicitParam 里面 name 是 字段名称，value是字段描述
		@ApiImplicitParam(name="username", value="用户名"),
		@ApiImplicitParam(name="password", value="密码")
	})
	public User selectUserByUsernameAndPassword(String username, String password) {
		LOGGER.info("多个参数用 @ApiImplicitParams 包裹着 @ApiImplicitParam ");
		LOGGER.info("@ApiImplicitParam 里面的 name 是 字段名称，value是字段描述");
		return new User(1L, username, password);
	}
	
    @GetMapping("/{id}")
    @ApiOperation(value = "主键查询（DONE）")
    @ApiImplicitParam(name = "id", value = "用户编号")
    public User get(@PathVariable Long id) {
        LOGGER.info("单个参数直接用  @ApiImplicitParam , 也可以用@ApiImplicitParams() 包裹");
        return new User(id, "username1", "password1");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户（DONE）")
    @ApiImplicitParam(name = "id", value = "用户编号")
    public void delete(@PathVariable Long id) {
        LOGGER.info("单个参数用 ApiImplicitParam");
    }

    @PostMapping
    @ApiOperation(value = "添加用户（DONE）")
    public User post(@RequestBody User user) {
        LOGGER.info("如果是 POST PUT 带 @RequestBody 注解的不用写 @ApiImplicitParam, 全部参数都会扫描到");
        return user;
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "修改用户（DONE）")
    public void put(@PathVariable Long id, @RequestBody User user, String test) {
    	User user2 = user;
    	Long id2 = id;
    	String test2 = test;
        LOGGER.info("如果你不想写 @ApiImplicitParam 那么 swagger 使用方法上的参数名作为描述信息 如 test 没有任何修饰也可以被扫描到");
    }
}
